package com.sylingd.blog.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.preference.PreferenceFragment
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.View
import com.sylingd.blog.R
import com.sylingd.blog.lib.Setting
import com.sylingd.blog.BaseApplication


class SettingActivity: AppCompatActivity() {
	companion object {
		fun launch(context: Activity) {
			val intent = Intent(context, SettingActivity::class.java)
			context.startActivity(intent)
		}
	}

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)

		setContentView(R.layout.activity_setting)
		val toolbar = findViewById<Toolbar>(R.id.toolbar)
		setSupportActionBar(toolbar)

		supportActionBar?.setDisplayHomeAsUpEnabled(true)
		toolbar.setNavigationOnClickListener {
			finish()
		}

		BaseApplication.initSlidr(this)

		val t = fragmentManager.beginTransaction()
		t.replace(R.id.settingFragment, SettingFragment())
		t.commit()
	}

	class SettingFragment: PreferenceFragment() {
		override fun onCreate(savedInstanceState: Bundle?) {
			super.onCreate(savedInstanceState)
			preferenceManager.sharedPreferencesMode = Context.MODE_PRIVATE
			addPreferencesFromResource(R.xml.prefs)
		}

		override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
			super.onViewCreated(view, savedInstanceState)

			val prefContentTextSize = findPreference(Setting.NAME_CONTENT_TEXT_SIZE)
			prefContentTextSize.summary = Setting.get(Setting.NAME_CONTENT_TEXT_SIZE, Setting.DEFAULT_CONTENT_TEXT_SIZE)
			prefContentTextSize.setOnPreferenceChangeListener { _, newValue ->
				prefContentTextSize.summary = newValue.toString()
				true
			}

			findPreference("go_about").setOnPreferenceClickListener {
				AboutActivity.launch(this@SettingFragment.activity)
				true
			}
		}
	}

}
